package com.zn.opit.quartzdemo.controller;

import com.zn.opit.quartzdemo.domain.R;
import com.zn.opit.quartzdemo.domain.cons.CommonConstants;
import com.zn.opit.quartzdemo.domain.entity.Qrtz;
import com.zn.opit.quartzdemo.exception.QuartzDemoException;
import com.zn.opit.quartzdemo.exception.QuartzDemoExceptionTypes;
import com.zn.opit.quartzdemo.service.QrtzService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * quartz定时任务控制器
 */
@Slf4j
@RestController
@RequestMapping(value = "qrtz")
public class QrtzController {

    @Resource
    private Scheduler scheduler;
    @Resource
    private QrtzService qrtzService;

    // 查询所有定时任务
    @GetMapping
    public List<Qrtz> listAll(Qrtz qrtz) {
        return qrtzService.listAllQrtz(qrtz);
    }

    // 创建任务
    @PostMapping
    public R create(@RequestBody Qrtz qrtz) {
        log.info(CommonConstants.LOGGER_PATTERN, "新增", qrtz.getGroup(), qrtz.getName());
        // 创建定时任务
        Date date = qrtzService.createJob(qrtz);
        return R.ok("新增成功").setData(date);
    }

    // 修改定时任务
    @PutMapping
    public R modify(@RequestBody Qrtz qrtz) {
        // 删除旧任务
        String oldName = (String) qrtz.getExtParams().get("oldName");
        String oldGroup = (String) qrtz.getExtParams().get("oldGroup");
        if (StringUtils.hasText(oldGroup)) {
            JobKey key = new JobKey(oldName, oldGroup);
            try {
                if (!scheduler.deleteJob(key)) {
                    throw new QuartzDemoException(QuartzDemoExceptionTypes.JOB_MODIFY_FAILED);
                }
            } catch (SchedulerException e) {
                throw new QuartzDemoException(QuartzDemoExceptionTypes.JOB_MODIFY_FAILED);
            }
        }
        log.info(CommonConstants.LOGGER_PATTERN, "删除成功", oldName, oldGroup);
        // 创建新的定时任务
        Date date = qrtzService.createJob(qrtz);
        return R.ok("修改成功").setData(date);
    }

    // 移除任务
    @DeleteMapping
    public R remove(String group, String name) {
        log.info(CommonConstants.LOGGER_PATTERN, "移除", name, group);
        qrtzService.remove(name, group);
        return R.ok("移除成功");
    }

    // 触发任务
    @PutMapping("/trigger")
    public R trigger(@RequestBody Qrtz qrtz) {
        try {
            JobKey key = new JobKey(qrtz.getName(), qrtz.getGroup());
            scheduler.triggerJob(key);
        } catch (SchedulerException e) {
            throw new QuartzDemoException(QuartzDemoExceptionTypes.JOB_TRIGGER_FAILED);
        }
        return R.ok("任务已触发");
    }

    // 暂停任务
    @PutMapping(value = "pause")
    public R pause(@RequestBody Qrtz qrtz) {
        log.info(CommonConstants.LOGGER_PATTERN, "暂停", qrtz.getName(), qrtz.getGroup());
        try {
            JobKey key = new JobKey(qrtz.getName(), qrtz.getGroup());
            scheduler.pauseJob(key);
        } catch (SchedulerException e) {
            throw new QuartzDemoException(QuartzDemoExceptionTypes.JOB_PAUSE_FAILED);
        }
        return R.ok("任务暂停");
    }

    // 恢复任务
    @PutMapping("/resume")
    public R resume(@RequestBody Qrtz qrtz) {
        log.info(CommonConstants.LOGGER_PATTERN, "恢复", qrtz.getGroup(), qrtz.getName());
        try {
            JobKey key = new JobKey(qrtz.getName(), qrtz.getGroup());
            scheduler.resumeJob(key);
        } catch (SchedulerException e) {
            throw new QuartzDemoException(QuartzDemoExceptionTypes.JOB_RESUME_FAILED);
        }
        return R.ok("任务恢复");
    }

}
